package com.yjhb.android.sbz.activity

import android.graphics.Color
import com.bigkoo.pickerview.builder.TimePickerBuilder
import com.bigkoo.pickerview.listener.OnTimeSelectListener
import com.yjhb.android.sbz.R
import com.yjhb.android.sbz.adapter.ExportAdapter
import com.yjhb.android.sbz.entity.ExportData
import com.yjhb.android.sbz.entity.ManagerTask
import com.yjhb.android.sbz.manager.ResManager
import com.yjhb.android.sbz.mvp.export.ExportPresenter
import com.yjhb.android.sbz.mvp.export.ExportView
import com.jojo.android.zxlib.tool.ZxSpanTool
import com.jojo.android.zxlib.tool.ZxTimeTool
import com.jojo.android.zxlib.tool.ZxToast
import kotlinx.android.synthetic.main.activity_export_data.*
import java.util.*


/**
 *  创建者 zx
 *  创建时间 2021/3/17 15:16
 */
class ExportDataActivity : BaseActivity<ExportPresenter>(), ExportView {
    var managerTask: ManagerTask? = null
    var startTime: Date? = null
    var endTime: Date? = null
    var exportAdapter: ExportAdapter? = null
    private val exportData = ArrayList<ExportData>()

    override fun initPresenter(): ExportPresenter? {
        return ExportPresenter(this)
    }

    override fun getLayoutId() = R.layout.activity_export_data;

    override fun initUI() {
        managerTask = intent.getSerializableExtra("managerTask") as ManagerTask?
        managerTask?.let {
            title_tv.text = "数据导出（${it.task.name}）"
        }
        emptyRecyclerView.emptyView = layout_empty
        exportAdapter = ExportAdapter(this)
        exportAdapter?.let {
            it.data = exportData
            emptyRecyclerView.adapter = exportAdapter
        }
    }

    override fun initData() {
        managerTask?.task?.id?.let { presenter.exportInfo(it) }
    }

    override fun regEvent() {
        back.setOnClickListener { finish() }
        start_time_ll.setOnClickListener {
            timeSelected(OnTimeSelectListener { date, _ ->
                this.startTime = date
                start_time_tv.text = ZxTimeTool.getDate(date.time, "yyyy年MM月dd日 HH:mm")
                //每次选择起始时间后都要清空结束时间
                this.endTime = null
                end_time_tv.text = "请选择时间段的结束时间"
            }, startTime, 1)
        }
        end_time_ll.setOnClickListener {
            if (startTime == null) {
                ZxToast.showINFOBottomToast("请先选择开始时间！")
                return@setOnClickListener
            }
            timeSelected(OnTimeSelectListener { date, _ ->
                this.endTime = date
                end_time_tv.text = ZxTimeTool.getDate(date.time, "yyyy年MM月dd日 HH:mm")
            }, startTime, 2)
        }
        addTimer_tv.setOnClickListener {
            if (startTime == null || endTime == null) {
                ZxToast.showINFOBottomToast("请先选择时间范围！")
                return@setOnClickListener
            }
            managerTask?.let {
                presenter.successDataToExcel(it.task.id, ZxTimeTool.date2String(startTime),
                        ZxTimeTool.date2String(endTime))
            }

        }

    }

    private fun timeSelected(selectedListener: OnTimeSelectListener, defaultTime: Date?, type: Int) {
        //规定最多只能选择一个月内的数据
        val rangEndCalendar = Calendar.getInstance()
        val rangStartCalendar = Calendar.getInstance()
        //开始时间不能超过30天之前，结束时间不能早于开始时间
        when (type) {
            1 -> rangStartCalendar.add(Calendar.DAY_OF_MONTH, -30)
            2 -> rangStartCalendar.time = startTime
        }
        //时间选择器
        val pvTime = TimePickerBuilder(this, selectedListener)
                .setType(booleanArrayOf(true, true, true, true, true, false)) // 默认全部显示
                .setCancelColor(ResManager.getThemeColor())
                .setSubmitColor(ResManager.getThemeColor())
                .setRangDate(rangStartCalendar, rangEndCalendar)
                .isCyclic(true)
                .build()
        val time = Calendar.getInstance()
        if (defaultTime == null) {
            time.time = Date()
        } else {
            time.time = defaultTime
        }
        pvTime.setDate(time)
        pvTime.show()
    }

    override fun onFailure(code: Int, msg: String?) {
        loadingDialog.cancel()
        ZxToast.showINFOBottomToast(msg)
    }

    override fun onSubmitSuccess() {
        loadingDialog.cancel()
        initData()
        ZxToast.showINFOBottomToast("操作成功！")
    }

    override fun onWait(tips: String) {
        loadingDialog.setTipsText(tips)
        loadingDialog.show()
    }

    override fun onDataList(data: ArrayList<ExportData>) {
        this.exportData.clear()
        this.exportData.addAll(data)
        loadingDialog.cancel()
        exportAdapter?.notifyDataSetChanged()
    }

    override fun onLastCount(count: Int) {
        val text = "今日剩余导出次数:${count}次"
        val index = text.indexOf("$count")
        numTv.text = ZxSpanTool.ColorSpan(text, index, index + "$count".length, Color.RED)
    }
}